import axios from 'axios'
import store from "../store/index"
// create an axios instance
import { GetQueryString } from "../utils/utils"
export const request = axios.create({
  baseURL: '', // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 45000 // request timeout
})
// request interceptor
request.interceptors.request.use(
  config => {

    let token = uni.getStorageSync('newcert-mobile-token') ;

    if (token) {
      uni.setStorageSync('newcert-mobile-token',token);
      config.headers.Authorization = 'Bearer' + ' ' + token
    }
    uni.showLoading({
      title: '加载中'
    });
    return config
  },
  error => {
    // do something with request error
    console.log(error, 'err')
    return Promise.reject(error)
  }
)

// response interceptor
request.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    uni.hideLoading()
    const res = response.data
    // if the custom code is not 20000, it is judged as an error.
    if ((res.code == 200 || res.code == 304)) {
      return Promise.resolve(res)
      // if the custom code is not 20000, it is judged as an error.
    }else if(response.headers['content-type']=="application/pdf;charset=utf-8"){
      return Promise.resolve(res)
    } else {
      uni.showToast({
        title: res.msg,
        icon: 'none',
        mask: true,
        duration: 2000
      });
      return Promise.reject(res)
    }
  },
  error => {
    uni.hideLoading()
    if (error.response) {
      switch (error.response.status) {
        case 400:
          uni.showToast({
            title: error.response.data.msg,
            icon: 'none',
            mask: true,
            duration: 2000
          });
          return Promise.reject(error.response);
          break
        case 401:
        case 416:
          uni.clearStorageSync();
          uni.redirectTo({
            url:'/pages/auth/no'
          })
          break
        case 404:
          uni.showToast({
            title: '网络请求不存在!',
            icon: 'none',
            mask: true,
            duration: 2000
          });
          break
        default:
          uni.showToast({
            title: error.response.data.message,
            icon: 'none',
            mask: true,
            duration: 2000
          });
      }
    } else {
      // 请求超时或者网络有问题
      if (error.message.includes('timeout')) {
        uni.showToast({
          title: '请求超时！请检查网络是否正常',
          icon: 'none',
          mask: true,
          duration: 2000
        });
      } else {
        uni.showToast({
          title: '请求失败，请检查网络是否已连接',
          icon: 'none',
          mask: true,
          duration: 2000
        });
      }
    }
    return Promise.reject(error)
  }
)


